package com.droidexchange.main;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.sf.stackwrap4j.entities.Question;
import net.sf.stackwrap4j.enums.Order;
import net.sf.stackwrap4j.query.QuestionQuery;
import net.sf.stackwrap4j.query.UnansweredQuery;
import net.sf.stackwrap4j.query.sort.BaseSort;
import android.content.res.Resources;
import com.droidexchange.R;
import com.droidexchange.util.Const;
import com.droidexchange.util.Util;

public class QFilter
{
	private static QFilter		instance			= null;
	private int					pageSize			= 10;
	private int					currPage			= 0;
	public String				strTitle			= null;

	public static final int		TYPE_LIST			= 0;
	public static final int		TYPE_SORT			= 1;
	public static final int		TYPE_ORDER			= 2;
	public static final int		TYPE_TAGS			= 3;

	private static final String	PREF_TYPE_LIST	= "pref_filter_list";
	private static final String	PREF_TYPE_SORT	= "pref_filter_sort";
	private static final String	PREF_TYPE_ORDER	= "pref_filter_order";
	private static final String	PREF_TYPE_TAGS	= "pref_filter_tags";

	private ArrayList<String>	listTypes	= new ArrayList<String>();
	private ArrayList<String>	sortTypes	= new ArrayList<String>();
	private ArrayList<String>	orderTypes	= new ArrayList<String>();

	public static QFilter getQFilter()
	{
		if (instance == null)
			instance = new QFilter();

		return instance;
	}

	private QFilter()
	{
	}

	public void init(Resources res)
	{
		listTypes = new ArrayList<String>(Arrays.asList(res.getStringArray(R.array.type_labels)));
		sortTypes = new ArrayList<String>(Arrays.asList(res.getStringArray(R.array.sort_by_labels)));
		orderTypes = new ArrayList<String>(Arrays.asList(res.getStringArray(R.array.order_by_labels)));
	}

	public void clearList()
	{
		currPage = 0;
	}
	
	public List<Question> getQuestionsList(boolean startAgain)
	{
		if (startAgain)
			currPage = 0;

		currPage++;
		List<Question> questions = null;
		Util.log("setQuestionsList : currPage = " + currPage);
		try
		{
			UnansweredQuery query = new UnansweredQuery();
			query.setComments(false).setBody(false).setAnswers(false);
			query.setPageSize(pageSize).setPage(currPage);

			String tags = getTags();
			if (tags != null)
				query.setTags(tags);

			query.setSort(getSort());
			query.setOrder(isDescending() ? Order.DESC : Order.ASC);

			questions = getQuestionsByListType(query);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return null;
		}

		return questions;
	}

	public String getLabelFromValue(int type, int num)
	{
		if (type == TYPE_LIST)
			return listTypes.get(num);
		else if (type == TYPE_SORT)
			return sortTypes.get(num);
		else if (type == TYPE_ORDER)
			return orderTypes.get(num);

		return null;
	}

	public int getValueFromLabel(int type, String label)
	{
		if (type == TYPE_LIST)
			return listTypes.indexOf(label);
		else if (type == TYPE_SORT)
			return sortTypes.indexOf(label);
		else if (type == TYPE_ORDER)
			return orderTypes.indexOf(label);

		return -1;
	}

	public String[] getLabelsOfType(int type)
	{
		if (type == TYPE_LIST)
			return (String[])listTypes.toArray(new String [listTypes.size ()]);
		else if (type == TYPE_SORT)
			return (String[])sortTypes.toArray(new String [sortTypes.size ()]);
		else if (type == TYPE_ORDER)
			return (String[])orderTypes.toArray(new String [orderTypes.size ()]);

		return null;
	}

	public String getCurrLabel(int type)
	{
		return getLabelFromValue(type, getCurrValue(type));
	}

	public int getCurrValue(int type)
	{
		if (type == TYPE_LIST)
			return Const.getPref(PREF_TYPE_LIST, 0);
		else if (type == TYPE_SORT)
			return Const.getPref(PREF_TYPE_SORT, 0);
		else if (type == TYPE_ORDER)
			return Const.getPref(PREF_TYPE_ORDER, 0);

		return -1;
	}

	public String getCurrStringValue(int type)
	{
		if (type == TYPE_TAGS)
			return Const.getPref(PREF_TYPE_TAGS, null);

		return null;
	}

	// returns true if value changed
	public boolean setCurrValue(int type, int val)
	{
		int oldVal = getCurrValue(type);
		if (oldVal == val)
			return false;

		if (type == TYPE_LIST)
			Const.putPref(PREF_TYPE_LIST, val);
		else if (type == TYPE_SORT)
			Const.putPref(PREF_TYPE_SORT, val);
		else if (type == TYPE_ORDER)
			Const.putPref(PREF_TYPE_ORDER, val);

		return true;
	}

	// returns true if value changed
	public boolean setCurrValue(int type, String val)
	{
		String oldVal = getCurrStringValue(type);
		if (oldVal != null && oldVal.equals(val))
			return false;

		if (type == TYPE_TAGS)
			Const.putPref(PREF_TYPE_TAGS, val);

		return true;
	}

	private List<Question> getQuestionsByListType(UnansweredQuery qQuery) throws Exception
	{
		int type = getCurrValue(TYPE_LIST);
		if (type == 0)
		{
			strTitle = "All Questions";
			return Util.sw.listQuestions(qQuery);
		}
		else if (type == 1)
		{
			strTitle = "Unanswered Questions";
			return Util.sw.listUnansweredQuestions(qQuery);
		}
		else if (type == 2)
		{
			strTitle = "All Questions";
			return Util.sw.listQuestions(qQuery);
		}
		else if (type == 3)
		{
			strTitle = "All Questions";
			return Util.sw.listQuestions(qQuery);
		}
		else if (type == 4)
		{
			strTitle = "All Questions";
			return Util.sw.listQuestions(qQuery);
		}
		else if (type == 5)
		{
			strTitle = "All Questions";
			return Util.sw.listQuestions(qQuery);
		}
		else
		{
			strTitle = "All Questions";
			return Util.sw.listQuestions(qQuery);
		}
	}

	private String getTags()
	{
		String tags = getCurrStringValue(TYPE_TAGS);
		if (tags == null || tags.length() <= 0)
			return null;
		
		return tags.replace(',', ';');
	}
	
	private BaseSort getSort()
	{
		int sort = getCurrValue(TYPE_SORT);
		if (sort == 0)
			return QuestionQuery.Sort.creation();
		else if (sort == 1)
			return QuestionQuery.Sort.activity();
		else if (sort == 2)
			return QuestionQuery.Sort.featured();
		else if (sort == 3)
			return QuestionQuery.Sort.votes();
		else if (sort == 4)
			return QuestionQuery.Sort.hot();
		else if (sort == 5)
			return QuestionQuery.Sort.week();
		else
			return QuestionQuery.Sort.month();
	}

	public boolean isDescending()
	{
		return (getCurrValue(TYPE_ORDER) == 0) ? true : false;
	}
}
